import Vue from 'vue'
import Vuex from 'vuex'
import { Dialog } from 'vant'
import { judgeCurrentTime, formatterTime } from '@/tools/index';
import createPersistedState from "vuex-persistedstate"
import {judgeEquipment} from '@/tools/utils'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    maskIsShow: false,
    timeState: '', // 当前时间状态，如上午、下午、晚上
    popoverShow: false,
    catRestStatus: 'awake',
    show_count_down: false,
    bubbleStatus: false,
    allData: {},
    serverTime: '',
    token: '',
    unionKey: '',
    platform: 0,
    isAlways: false,
    showOption: false,
    deviceEnvironment: judgeEquipment()
  },
  mutations: {
    controlMask(state, payload) {
      state.maskIsShow = payload.maskIsShow
    },
    JUDGE_CURRENT_TIME_STATE(state) {
      // state.timeState = { name: '早上好', value: 0 };
      state.timeState = judgeCurrentTime();
    },
    CONTROL_POPOVER (state, payload) {
      state.popoverShow = payload.popoverShow
    },
    CHANGE_CAT_REST_STATUS (state, payload) {
      state.catRestStatus = payload.state
    },
    GET_COUNT_DOWN_STATUS (state, payload) {
      state.show_count_down = payload.state
    },
    GET_BUBBLE_STATUS (state, payload) {
      state.bubbleStatus = payload.state
    },
    SAVE_DATA (state, payload) {
      state.allData = payload
    },
    SAVE_SERVER_TIME (state, payload) {
      state.serverTime = payload
    },
    SAVE_REQUEST_PARAMS (state, payload) {
      state.token = payload.token || ''
      state.unionKey = payload.unionKey || '',
      state.platform = payload.platform || 0
    },
    TITLE_CONFIG (state, payload) {
      state.title = payload.title
      state.isAlways = payload.isAlways
    },
    SEND_TOKEN_TO_APP (state, payload) {
      state.token = payload.token
      window.WebViewJavascriptBridge && window.WebViewJavascriptBridge.callHandler('alterToken', {token: payload.token}, () => {

      })
    },
    GET_TOKEN_FROM_APP (state) {
      window.WebViewJavascriptBridge && window.WebViewJavascriptBridge.callHandler('getToken', {}, (data) => {
        // Dialog.alert({
        //   title: '提示',
        //   message: JSON.stringify(data),
        // })
        state.token = data.token
      })
    },
    CLOSE_MENU (state, payload) {
      state.showOption = payload.showOption
    },
    GET_DEVICE_ENV (state, payload) {
      state.deviceEnvironment = payload.deviceEnvironment
    }
  },
  plugins: [createPersistedState({ storage: window.sessionStorage })]
})